if( SERVER ) then

	AddCSLuaFile( "shared.lua" );

end

if( CLIENT ) then

	SWEP.PrintName = "Keypad Cracker";
	SWEP.Slot = 4;
	SWEP.SlotPos = 3;
	SWEP.DrawAmmo = false;
	SWEP.DrawCrosshair = true;

end

// Variables that are used on both client and server

SWEP.Author			= "lilEzek"
SWEP.Instructions	= "Click once on a Keypad to start cracking. You need to aim the keypad or it will cancel."
SWEP.Contact		= ""
SWEP.Purpose		= "Crack keypads"

SWEP.ViewModelFOV	= 60
SWEP.ViewModelFlip	= false

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.NextStrike = 0;
  
SWEP.ViewModel = Model( "models/weapons/v_c4.mdl" );
SWEP.WorldModel = Model( "models/weapons/w_c4.mdl" );
  
SWEP.Sound = Sound( "weapons/deagle/deagle-1.wav" );
  
SWEP.Primary.ClipSize		= -1					// Size of a clip
SWEP.Primary.DefaultClip	= 0				// Default number of bullets in a clip
SWEP.Primary.Automatic		= false				// Automatic/Semi Auto
SWEP.Primary.Ammo			= ""

SWEP.Secondary.ClipSize		= -1			// Size of a clip
SWEP.Secondary.DefaultClip	= 0				// Default number of bullets in a clip
SWEP.Secondary.Automatic	= false				// Automatic/Semi Auto
SWEP.Secondary.Ammo			= ""
SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true
SWEP.IsCracking = false
SWEP.WillEndCrack = 0

/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack( )
   Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
	if self.IsCracking then
		return
	end
	trace = self.Owner:GetEyeTrace()
	owner = self.Owner
	if trace.Entity:GetClass() != "sent_keypad" and trace.Entity:GetClass() != "sent_keypad_wire" then
		return false
	end
	if trace.HitPos:Distance(trace.StartPos) > 300 or !owner:Alive() then
		return false
	end
	self.IsCracking = true
	self.WillEndCrack = CurTime() + 30
	if SERVER then
		for k,v in pairs(Keypad.Passwords) do
			if v.Ent == trace.Entity:EntIndex() then
				owner.keypadcracking = v
			end
		end
		for k,v in pairs(KeypadWire.Passwords) do
			if v.Ent == trace.Entity:EntIndex() then
				owner.keypadcracking = v
			end
		end
	end
	if CLIENT then
		owner.keypadcracking = trace.Entity:EntIndex()
	end
end
 
/*---------------------------------------------------------
  SecondaryAttack
  ---------------------------------------------------------*/
function SWEP:SecondaryAttack()

end
  
function SWEP:Reload()
end

function SWEP:Think()
	if !self.IsCracking then
		return
	end
	trace = self.Owner:GetEyeTrace()
	owner = self.Owner
	if SERVER then
		if !owner:Alive() or trace.HitPos:Distance(trace.StartPos) > 300 then
			return false
		end
		if CurTime() >= self.WillEndCrack then
			owner:PrintMessage(HUD_PRINTCENTER,tostring(owner.keypadcracking.Pass))
			self.WillEndCrack = 0
			self.IsCracking = false
		end
		if trace.Entity:EntIndex() != owner.keypadcracking.Ent then
			self.WillEndCrack = 0
			self.IsCracking = false
			owner:PrintMessage(HUD_PRINTCENTER,"You canceled.")
		end
	end
	if CLIENT then
		if CurTime() >= self.WillEndCrack then
			self.WillEndCrack = 0
			self.IsCracking = false
		end
		if trace.Entity:EntIndex() != owner.keypadcracking then
			self.WillEndCrack = 0
			self.IsCracking = false
		end
	end
end

function SWEP:DrawHUD()
	if self.IsCracking then
		surface.SetDrawColor( 255, 0, 0, 255) --Red
		surface.DrawRect((ScrW()-(self.WillEndCrack - CurTime())*5)/2, (ScrH()-50)/2,(self.WillEndCrack - CurTime())*5, 50 )
	end
end

//hook.Add("Think","DrawHUDKC",DrawHUD)


